<?php

namespace brand\components;

use brand\logic\BrandHelper;
use brand\logic\Database;

class Slider extends Component
{

    public function _init()
    {
        $this->component = 'slider';
    }

    /**
     * 表单值字段过滤
     */
    public function checkFormValue()
    {

        if ($this->getFormParam('props.range')){
            $this->value = BrandHelper::string2array($this->value);
        }
    }



    /**
     * 插入验证
     * @param $key
     * @param $value
     */
    public function checkUpdateData($key, $value)
    {
        return BrandHelper::array2String($value);
    }

    /**
     * 创建Curd表单
     * @param $field
     * @param $props
     */
    public function DbToCurd($field,$props = [])
    {
        $column = \brand\Brand::Builder()->form_props($props);
        $comment = Database::curdComment($field['comment']);

        if($comment['options']){

            $options['min'] = (float)$comment['options']['min'] ?? 0;
            $options['max'] = (float)$comment['options']['max'] ?? 100;
            $options['range'] = (isset($comment['options']['range']) && $comment['options']['range']);

            $column->form_method('slider')->form_props($options);

            if ($options['range']){
                $column->table('range',true);
            }
        }

        return $column;
    }

    public function buildConfigColumn($tableConfigValue, $curd_method = 'curd_method')
    {
        $options['min'] = \brand\Brand::Builder('最小值','number')->build();
        $options['max'] = \brand\Brand::Builder('最大值','number')->build();
        $options['range'] = \brand\Brand::Builder('区间选择','radio')->form_options(BrandHelper::formatOptions(['否','是']))->build();
        $cloumn = \brand\Brand::Builder('选择器配置','arrays')
            ->form_options([$options])
            ->form_props( [
                'append'    =>  $curd_method != 'curd_method'
            ])
            ->form_visible([['prop' => $curd_method, 'value' => 'Slider']])
            ->build();


        $props = $tableConfigValue['form']['props'] ?? [];
        if (isset($props['range']) && $props['range']){
            $props['range'] = 1;
        }else{
            $props['range'] = 0;
        }

        $values[] = $props;

        return ['column' => $cloumn,'value' => $values];
    }

    public function methodFormConf($form,$table,$params)
    {
        $param = $params['param'][0] ?? [];


        $props['min'] = $param['min'] ?? 0;
        $props['max'] = $param['max'] ?? 100;
        $props['range'] = isset($param['range']) && $param['range'] == 1;

        $form['props'] = $props;


        return [$form,$table];
    }

}